package net.medplus.social.weixin;

import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import static net.medplus.social.weixin.Utils.checkNotNull;

/**
 * Description:
 *
 * @author: Xian XueLiang
 * @date: 2016/6/12
 */
public class RestAllinSignIn {
    private final SHARE_MEDIA shareMedia;
    private final String appId;
    private final String appSecret;
    private final UMAuthListener umAuthListener;

    RestAllinSignIn(SHARE_MEDIA shareMedia, String appId, String appSecret, UMAuthListener umAuthListener) {
        this.shareMedia = shareMedia;
        this.appId = appId;
        this.appSecret = appSecret;
        this.umAuthListener = umAuthListener;
    }
    public <T> T create(final Class<T> services){
        Utils.validateServiceInterface(services);
        return (T) Proxy.newProxyInstance(services.getClassLoader(), new Class[]{services}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                if (method.getDeclaringClass()==Object.class) {
                    return method.invoke(this, args);
                }

                return null;
            }
        });
    }

    public static final class Builder {
        private SHARE_MEDIA shareMedia;
        private String appId;
        private String appSecret;
        private UMAuthListener umAuthListener;

        public Builder() {
        }

        public Builder basePlatform(SHARE_MEDIA sm) {
            this.shareMedia = checkNotNull(sm, "share_media == null");
            return this;
        }

        public Builder addAppId(String appId) {
            this.appId = checkNotNull(appId, "appId == null");
            return this;
        }

        public Builder addAppSecret(String appSecret) {
            this.appSecret = checkNotNull(appSecret, "appSecret == null");
            return this;
        }

        public Builder addListener(UMAuthListener umAuthListener) {
            this.umAuthListener = checkNotNull(umAuthListener, "umAuthListener must be implemented by subClass");
            return this;
        }

        public RestAllinSignIn build() {
            if (shareMedia == null) {
                throw new IllegalStateException("Share_Media is required!!");
            }

            if (appId == null || appSecret == null) {
                PlatformConfig.Platform platform = PlatformConfig.getPlatform(shareMedia);
                if (!platform.isConfigured()) {
                    throw new IllegalStateException("AppId & AppSecret is required!! ");
                }
            }
            if (umAuthListener == null) {
                throw new IllegalStateException("must implement UMAuthListener");
            }
            return new RestAllinSignIn(shareMedia, appId, appSecret, umAuthListener);
        }
    }
}
